package com.hotacorp.opencloud.userservice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.AddGroup;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.UpdateGroup;
import com.hotacorp.opencloud.userservice.entity.SysRoleEntity;
import com.hotacorp.opencloud.userservice.service.SysRoleMenuService;
import com.hotacorp.opencloud.userservice.service.SysRoleService;

/**
 * 角色
 *
 * @author lwg
 * @email lwg@gmail.com
 * @date 2019-12-25 11:52:19
 */
@RestController
public class SysRoleController extends AbstractController {
    @Autowired
    private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

    /**
     * 列表
     */
    @RequestMapping(value = "/sysrole/list", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysrole:list')")
    public Result<?> list(@RequestParam Map<String, Object> params){
    	params.put("tenantid", getTenantid());
        PageData page = sysRoleService.queryPage(params);

        return Result.succeed("succeed", page);
    }

	/**
	 * 选择角色(添加、修改用户)
	 */
	@GetMapping("/sysrole/select")
	@PreAuthorize("hasAuthority('userservice:sysrole:select')")
	public Result<?> select(){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("tenantid", getTenantid());
        List<SysRoleEntity> menuList = sysRoleService.select(params);
		
		return Result.succeed("succeed", menuList);
	}


    /**
     * 信息
     */
    @RequestMapping(value = "/sysrole/info/{roleId}", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysrole:info')")
    public Result<?> info(@PathVariable("roleId") Long roleId){
		SysRoleEntity sysRole = sysRoleService.getById(roleId);
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId, getTenantid());
		sysRole.setMenuIdList(menuIdList);

        return Result.succeed("succeed", sysRole);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/sysrole/save", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysrole:save')")
    public Result<?> save(@RequestBody @Validated(AddGroup.class) SysRoleEntity sysRole){
		sysRoleService.saveRole(sysRole, getTenantid());

        return Result.succeed();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/sysrole/update", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysrole:update')")
    public Result<?> update(@RequestBody @Validated(UpdateGroup.class) SysRoleEntity sysRole){
		sysRoleService.update(sysRole, getTenantid());

        return Result.succeed();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/sysrole/delete", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysrole:delete')")
    public Result<?> delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds, getTenantid());

        return Result.succeed();
    }

}
